package cn.bookcycle.requestroom.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.bookcycle.requestroom.ConstantData.BusinessConstant;
import cn.bookcycle.requestroom.ConstantData.DBConstant;
import cn.bookcycle.requestroom.pojo.Authorization;
import cn.bookcycle.requestroom.pojo.DateRecord;
import cn.bookcycle.requestroom.pojo.RoomInfo;
import cn.bookcycle.requestroom.pojo.UserInfo;


/**
 * 申请看房工具类
 *
 * @author liufenglin
 * @date 2018年3月4日
 */
public class ApplyRoom {
	private static final Logger logger = LoggerFactory.getLogger(ApplyRoom.class);
	/**
	 * 为用户申请免费看房
	 * 
	 * @param user
	 *            用户实体
	 * @param room
	 *            房间实体
	 * @param dateRecord
	 *            {@linkplain DateRecord}
	 * @return
	 */
	public static String applyRoomFree(UserInfo user, RoomInfo room, DateRecord dateRecord) {
		logger.info("为用户申请免费查看");
		String result = "";

		if (room.isEmpty()) {
			if (user.getUsedFreeCount() > BusinessConstant.TOTAL_FREE) {
				// 返回40007，用户免费看房次数不足
				result = BusinessConstant.FREE_CHANCE_NOT_ENOUGH;
			} else {
				// 为用户请求临时查看权限
				applyAuthorization(user.getUserId(), room.getRoomId(), dateRecord);
				// 判断临时查看权限是否获取成功，目前都是成功
				result = BusinessConstant.SUCCESS_FREE;

				/// 目前不存在请求部成功的情况
				// 请求临时查看权限不成功
				// result = BusinessConstant.FAIL;

				// 修改数据库中的用户免费看房次数
				DaoUtil.updateFreeChance(user);
			}
		} else {
			// 房间不为空，拒绝看房
			result = BusinessConstant.ROOM_FILL;
		}
		return result;
	}

	/**
	 * 为用户申请付费看房机会
	 * 
	 * @param user
	 *            用户实体
	 * @param room
	 *            房间实体
	 * @param dateRecord
	 *            {@linkplain DateRecord}
	 * @return
	 */
	public static String applyRoomFee(UserInfo user, RoomInfo room, DateRecord dateRecord) {
		logger.info("为用户申请付费查看");
		String result = "";

		if (room.isEmpty()) {
			// 为用户请求临时查看权限
			applyAuthorization(user.getUserId(), room.getRoomId(), dateRecord);
			// 判断临时查看权限是否获取成功
			result = BusinessConstant.SUCCESS_FEE;

			/// 目前不存在请求部成功的情况
			// 请求临时查看权限不成功
			// result = BusinessConstant.FAIL;

		} else {
			// 房间不为空，拒绝看房
			result = BusinessConstant.ROOM_FILL;
		}
		return result;
	}

	/**
	 * 为用户请求临时查看权限
	 * 
	 * @param userId
	 *            用户ID
	 * @param roomId
	 *            房间ID
	 * @param dateRecord
	 *            {@linkplain DateRecord}
	 */
	private static String applyAuthorization(String userId, String roomId, DateRecord dateRecord) {
		logger.info("为用户申请查看权限");
		String result = "";
		// 各种参数组成的map
		Map<String, Object> map = new HashMap<String, Object>();
		// 各种授权参数组成的map
		Map<String, Object> authmap = new HashMap<String, Object>();
		// 指定数据库
		map.put(DBConstant.DB_NAME, DBConstant.USER_ROOM_DB);
		// 指定表
		map.put(DBConstant.TAB_NAME, DBConstant.CHECK_AUTHORIZATION_TAB);
		// 指定insert方法
		map.put(DBConstant.METHOD, DBConstant.INSERT);
		// 建立授权信息
		Authorization authorization = new Authorization(userId, roomId, dateRecord);
		authmap.put(DBConstant.USER_ID_T, userId);
		authmap.put(DBConstant.ROOM_ID_T, roomId);
		authmap.put(DBConstant.DATE, dateRecord.getStartDate());
		authmap.put(DBConstant.DURATION, dateRecord.getDuration());
		map.put(DBConstant.AUTHORIZATION, authmap);
		// 向数据库微服务发送请求
		result = DBUtil.sendPost(DBConstant.DB_URI, DBConstant.DATA, new JSONObject(map).toString(), DBConstant.UTF8);
		logger.info("向数据库微服务请求临时查看权限后，返回的信息是：{}", result);
		return result;
	}
}
